#include "updaterio.h"

UpdaterIO::UpdaterIO()
{
}

void UpdaterIO::exportInfo(const InputData &data, const QString &filename)
{
  QSettings set(filename, QSettings::IniFormat);

  set.beginGroup("UpdaterInfo");
  set.setValue("Server", data.server);
  set.setValue("VersionFile", data.version_file);
  set.setValue("LocalDir", data.local_dir);
  set.setValue("Architecture", data.architecture);
  set.setValue("CurrentVersion", data.current_version);
  set.setValue("RunAfter", data.run_after);
  set.setValue("RunAfterDir", data.run_after_dir);
  set.beginWriteArray("PathSubst", data.path_subst.count());
  QMap<QString,QString>::const_iterator it, it_end = data.path_subst.end();
  int i = 0;
  for (it = data.path_subst.begin(); it != it_end; it++)
  {
    set.setArrayIndex(i++);
    set.setValue("Key", it.key());
    set.setValue("Path", it.value());
  }
  set.endArray();
  set.endGroup();
}

void UpdaterIO::importInfo(InputData &data, const QString &filename)
{
  QSettings set(filename, QSettings::IniFormat);

  set.beginGroup("UpdaterInfo");
  data.server = set.value("Server", data.server).toString();
  data.version_file = set.value("VersionFile", data.version_file).toString();
  data.local_dir = set.value("LocalDir", data.local_dir).toString();
  data.architecture = set.value("Architecture", data.architecture).toString();
  data.current_version = set.value("CurrentVersion", data.current_version).toInt();
  data.run_after = set.value("RunAfter", data.run_after).toString();
  data.run_after_dir = set.value("RunAfterDir", data.run_after_dir).toString();

  int cnt = set.beginReadArray("PathSubst");
  data.path_subst.clear();
  for (int i = 0; i < cnt; i++)
  {
    set.setArrayIndex(i);
    QString key = set.value("Key").toString();
    QString val = set.value("Path").toString();
    if (key.size() && val.size())
      data.path_subst[key] = val;
  }
  set.endArray();

  set.endGroup();
}
